<?php 
function pe_wx($reurl)
{
	$appid = "wxd8634ee3e5ae23de";
	$secret = "68e6ea5ff617defda176720b7acf6076";
	if(!isset($_GET['code']) || empty($_GET['code'])){
		$REDIRECT_URI = $reurl; 
		$url1 = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=".$appid."&redirect_uri=".urlencode($REDIRECT_URI)."&response_type=code&scope=snsapi_base&state=123#wechat_redirect";
		header("Location:".$url1); 
		exit;
	}
	$code =	trim($_GET['code']);
	$getTokenUrl = "https://api.weixin.qq.com/sns/oauth2/access_token".
	                            "?appid=".$appid.
	                            "&secret=".$secret.
	                            "&code={$code}".
	                            "&grant_type=authorization_code";
	$ch1 = curl_init();
	curl_setopt($ch1, CURLOPT_URL,$getTokenUrl);
	curl_setopt($ch1, CURLOPT_HEADER,0);
	curl_setopt($ch1, CURLOPT_RETURNTRANSFER,1);//禁止调用时就输出获取到的数据
	curl_setopt($ch1, CURLOPT_FOLLOWLOCATION,1);
	curl_setopt($ch1, CURLOPT_SSL_VERIFYPEER,false);
	curl_setopt($ch1, CURLOPT_SSL_VERIFYHOST,false);
	$result1 = curl_exec($ch1);
	curl_close($ch1);
	$result1 = json_decode($result1);
	$openid =  $result1->openid;
	
	$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$secret;
	$ch = curl_init();
	curl_setopt($ch, CURLOPT_URL,$url);
	curl_setopt($ch, CURLOPT_HEADER,0);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);//禁止调用时就输出获取到的数据
	curl_setopt($ch, CURLOPT_FOLLOWLOCATION,1);
	curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,false);
	curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,false);
	$result = curl_exec($ch);
	curl_close($ch);
	$result = json_decode($result);
	$access_token =  $result->access_token;
	
	$url2 = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=".$access_token."&openid=".$openid."&lang=zh_CN";
	$ch2 = curl_init();
	curl_setopt($ch2, CURLOPT_URL,$url2);
	curl_setopt($ch2, CURLOPT_HEADER,0);
	curl_setopt($ch2, CURLOPT_RETURNTRANSFER,1);//禁止调用时就输出获取到的数据
	curl_setopt($ch2, CURLOPT_FOLLOWLOCATION,1);
	curl_setopt($ch2, CURLOPT_SSL_VERIFYPEER,false);
	curl_setopt($ch2, CURLOPT_SSL_VERIFYHOST,false);
	$result2 = curl_exec($ch2);
	curl_close($ch2);
	$result2 = json_decode($result2);
	$openid =  $result2->openid;
	$nickname =  $result2->nickname;
	$headimgurl =  $result2->headimgurl;
	$_SESSION['wxid'] = $openid;
	$_SESSION['wxname'] = $nickname;
	$_SESSION['wximg'] = $headimgurl;
}
?>